﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using QuanLyQuanCafe.DataAccess;
using QuanLyQuanCafe.BusinessObject;

namespace QuanLyQuanCafe.BusinessLogic
{
    class CT_KhuyenMaiBL
    {
        KetNoiDuLieu connect = new KetNoiDuLieu();
        public DataTable DSCT_KM(string value)
        {
            string sql = "SELECT CT_KMBAN.Giam, KHUVUC.TenKV FROM KHUVUC, CT_KMBAN WHERE CT_KMBAN.MaKV = KHUVUC.MaKV AND CT_KMBAN.MaKM = '"+value+"'";
            return connect.LayDanhSachDuLieu(sql);
        }

        public float TyLeGiam(string value1, string value2)
        {
            float giam = 0;
            string sql = "SELECT GIAM FROM CT_KMBAN WHERE MaKV = '"+value1+"' AND MaKM = '" + value2 + "'";
            DataTable dt = connect.LayDanhSachDuLieu(sql);
            if(dt.Rows.Count>0)
            giam = float.Parse(dt.Rows[0][0].ToString());
            return giam;
        }
       
        public bool CheckCTKM(int value1)
        {
            if (connect.KiemTraGiaTriInt("CT_KMBAN", "MaKM", value1))
                return true;
            return false;
        }

        public bool CheckCTKV(int value1)
        {
            if (connect.KiemTraGiaTriInt("CT_KMBAN", "MaKV", value1))
                return true;
            return false;
        }


        public bool ThemCTKM(CT_KhuyenMaiBO ctkhuyenmaiBO)
        {
            if (TestSave(ctkhuyenmaiBO))
            {
                string sql = string.Format("INSERT INTO CT_KMBAN VALUES ('{0}', '{1}', '{2}')", ctkhuyenmaiBO.MaKM, ctkhuyenmaiBO.MaKV, ctkhuyenmaiBO.Giam);
                if (connect.ExecuteQuery(sql))
                {
                    MessageBox.Show("Thêm Thành Công!", "Quản Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return true;
                }
            }
            return false;
        }

        

        public bool XoaCTKM(string value1, string value2)
        {
            string sql = string.Format("DELETE FROM CT_KMBAN WHERE MaKV = '" + value1 + "' AND MaKM = '" + value2 + "'");
            if (connect.ExecuteQuery(sql))
            {
                MessageBox.Show("Xóa Thành Công!", "Quản Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return true;
            }
            return false;
        }

        public bool XoaCTMaKM(string value)
        {
            string sql = string.Format("DELETE FROM CT_KMBAN WHERE MaKM = '" + value + "'");
            if (connect.ExecuteQuery(sql))
            {
                return true;
            }
            return false;
        }

        public bool TestSave(CT_KhuyenMaiBO ctkhuyenmaiBO)
        {
            if (ctkhuyenmaiBO.Giam <= 0)
            {
                MessageBox.Show("Tỷ Lệ Giảm Phải Lớn Hơn 0!", "Quản Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (ctkhuyenmaiBO.Giam >100)
            {
                MessageBox.Show("Tỷ Lệ Giảm Phải Nhỏ Hơn 100!", "Quản Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

           

            return true;
        }
    }
}
